package com.huanzhidadi.canal.receiver;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

/**
 * canal从mysql数据库中同步数据至RabbitMQ
 */
@Slf4j
@Component
@RabbitListener(queues = "canal_queue")
public class CanalReceiver {

    @RabbitHandler(isDefault = true)
    public void process(Message message, Channel channel, String msg){
        JSONObject jsonObject = JSONObject.parseObject(msg);
        System.out.println(jsonObject);
        String table = jsonObject.getString("table");
        String type = jsonObject.getString("type");
        JSONArray jsonArray = jsonObject.getJSONArray("data");
        for (Object obj : jsonArray) {
            log.info("表：{}，类型：{}，内容：{}", table, type, obj);
        }
    }
}
